home *** CD-ROM | disk | FTP | other *** search
/ Transactor / Transactor_10_1986_Transactor_Publishing.d64 / string calc (.txt) < prev    next >
Commodore BASIC  |  2023-02-26  |  1KB  |  45 lines

  1. 10 printchr$(147)
  2. 20 input"enter formula";f$
  3. 30 iff$="stop"thenstop
  4. 40 a=0
  5. 50 f$=f$+ " ":p$=f$:id=9:dn=0
  6. 60 fori=1tolen(p$)
  7. 65 ifmid$(p$,i,1)=" "then80
  8. 70 next:print"?invalid formula":goto20
  9. 80 ifi=len(p$)thendn=1
  10. 90 x=i:x$=left$(p$,x)
  11. 100 va=1:v=val(x$)
  12. 105 ifval(x$)=0andleft$(x$,1)<>"0"thenva=0
  13. 110 ifleft$(x$,2)="pi"thenv=3.14159265:va=1
  14. 120 ifleft$(x$,1)="[255]"thenv=3.14159265:va=1
  15. 130 ifva=1then230
  16. 135 ifleft$(x$,1)="'"thenprint"st = ";a:goto310
  17. 140 id$=left$(x$,1):id=0
  18. 150 ifid$="+"thenid=1
  19. 160 ifid$="-"thenid=2
  20. 170 ifid$="*"thenid=3
  21. 180 ifid$="/"thenid=4
  22. 190 ifid$="^"thenid=5
  23. 195 if(id>0)and(id<90)andmid$(x$,2,1)<>" "thenprint"?error":goto20
  24. 200 ifid$=" "then310
  25. 203 ifa=0then210
  26. 204 ifid$="@"thenam=val(mid$(x$,2,1)):am=-am:goto207
  27. 205 if id$<>";"then210
  28. 206 am=val(mid$(x$,2,1)):ifa=0then210
  29. 207 a=int(a*(10^am)+.5):a=a/(10^am):id=0:goto310
  30. 210 ifid=0thenprint"?bad symbol":goto20
  31. 220 goto310
  32. 230 ifid=1thena=a+v
  33. 240 ifid=9thena=v:id=0:goto310
  34. 250 ifid=0thenprint"?error":(NULL)t20
  35. 260 ifid=2thena=a-v
  36. 270 ifid=3thena=a*v
  37. 280 ifid=5thena=a^v
  38. 290 ifid=4andv=0thenprint"?div by zero":goto20
  39. 300 ifid=4thena=a/v
  40. 310 ifdn=1then330
  41. 320 p$=right$(p$,(len(p$)-len(x$))):goto60
  42. 330 print"result = ";a:a=0:goto20
  43. 1000 rem notice this program is still subject to floating point
  44. 1010 rem round off errors and is only as accurate has basic permits
  45.